package org.pay.mch.settlement.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.pay.mch.common.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.RetEnum;
import org.pay.core.common.domain.BizResponse;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.common.domain.PayResponse;
import org.pay.core.common.util.MyLog;
import org.pay.core.entity.MchBankAccount;
import org.pay.mch.common.ctrl.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 17/12/7
 * @description:
 */
@RestController
@RequestMapping(Constant.MCH_CONTROLLER_ROOT_PATH + "/bank_account")
@PreAuthorize("hasRole('"+ MchConstant.MCH_ROLE_NORMAL+"')")
public class BankAccountController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    private static final MyLog _log = MyLog.getLog(BankAccountController.class);

    /**
     * 添加银行账户信息
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Byte accountType = getByteRequired(param, "accountType");
        String accountNo = getStringRequired(param, "accountNo");
        String remark = param.getString("remark");
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setMchId(getUser().getId());
        mchBankAccount.setName(getUser().getName());
        mchBankAccount.setAccountType(accountType);
        mchBankAccount.setAccountNo(accountNo);
        mchBankAccount.setRemark(remark);
        int count = rpcCommonService.rpcMchBankAccountService.add(mchBankAccount);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchBankAccount mchBankAccount = getObject(param, MchBankAccount.class);
        if(mchBankAccount == null) mchBankAccount = new MchBankAccount();
        mchBankAccount.setMchId(getUser().getId());
        int count = rpcCommonService.rpcMchBankAccountService.count(mchBankAccount);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchBankAccount> mchAccountHistoryList = rpcCommonService.rpcMchBankAccountService
                .select((getPageIndex(param)-1) * getPageSize(param), getPageSize(param), mchBankAccount);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchAccountHistoryList, count));
    }

    /**
     * 修改银行账户信息
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        String accountNo = getStringRequired(param, "accountNo");
        String remark = param.getString("remark");
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setId(id);
        mchBankAccount.setAccountNo(accountNo);
        mchBankAccount.setRemark(remark);
        int count = rpcCommonService.rpcMchBankAccountService.update(mchBankAccount);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 查询
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setMchId(getUser().getId());
        mchBankAccount.setId(id);
        mchBankAccount = rpcCommonService.rpcMchBankAccountService.find(mchBankAccount);
        return ResponseEntity.ok(PayResponse.buildSuccess(mchBankAccount));
    }

    /**
     * 查询默认账号
     * @return
     */
    @RequestMapping("/default_get")
    @ResponseBody
    public ResponseEntity<?> defaultAccount(HttpServletRequest request) {
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setMchId(getUser().getId());
        mchBankAccount.setIsDefault(MchConstant.PUB_YES);
        mchBankAccount = rpcCommonService.rpcMchBankAccountService.find(mchBankAccount);
        return ResponseEntity.ok(PayResponse.buildSuccess(mchBankAccount));
    }

    /**
     * 设置默认账号
     * @return
     */
    @RequestMapping("/default_set")
    @ResponseBody
    public ResponseEntity<?> setDefault(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        // 设置所有状态为非默认
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setMchId(getUser().getId());
        mchBankAccount.setIsDefault(MchConstant.PUB_NO);
        int count = rpcCommonService.rpcMchBankAccountService.update(mchBankAccount);
        _log.info("set all bankAccount default is no, id={}, result={}", id, count);
        // 设置当前为默认
        MchBankAccount mchBankAccount2 = new MchBankAccount();
        mchBankAccount2.setId(id);
        mchBankAccount2.setIsDefault(MchConstant.PUB_YES);
        count = rpcCommonService.rpcMchBankAccountService.update(mchBankAccount2);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 取消默认账号
     * @return
     */
    @RequestMapping("/default_cancel")
    @ResponseBody
    public ResponseEntity<?> cancelDefault(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        // 设置当前为非默认
        MchBankAccount mchBankAccount = new MchBankAccount();
        mchBankAccount.setId(id);
        mchBankAccount.setIsDefault(MchConstant.PUB_NO);
        int count = rpcCommonService.rpcMchBankAccountService.update(mchBankAccount);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

}
